﻿using MESPro.DBManager;
using MESPro.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace MESPro.Views
{
    public partial class PropertyFrm : DockContent
    {
        public PropertyFrm()
        {
            InitializeComponent();
        }

        internal void ShowObj(ListViewItem? item)
        {
            if (item == null)
            { this.propertyGrid1.SelectedObject = null; }
            else
            { this.propertyGrid1.SelectedObject = item.Tag; }

        }
        internal void ShowControl(object? item)
        {
            if (item == null)
            { this.propertyGrid1.SelectedObject = null; }
            else
            { this.propertyGrid1.SelectedObject = item; }

        }
        public Action RMUpdateableByObject { get; set; }
        public Action<object, MsgLevel> RMShowMsg { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            var obj = this.propertyGrid1.SelectedObject;
            if (obj is Control || obj is null) return;
            var res = DbContext.Db.UpdateableByObject(this.propertyGrid1.SelectedObject).ExecuteCommand();
            RMUpdateableByObject?.Invoke();
            RMShowMsg?.Invoke(res, MsgLevel.Trace);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var obj = this.propertyGrid1.SelectedObject;
            if (obj is Control control)
            {
                // var control = obj as Control;
                //  if (control != null)
                // {
                control.Parent.Controls.Remove(control);
                this.propertyGrid1.SelectedObject = null;
                // }

                return;
            }
            else
            {
                var ren = DbContext.Db.DeleteableByObject(this.propertyGrid1.SelectedObject).ExecuteCommand();
                MainServerFrm.RMShowMsg?.Invoke(ren > 0 ? "删除成功" : "删除失败", MsgLevel.Trace);// Show();
            }
        }
    }
}
